package com.ccl.keyassistant.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ccl.keyassistant.R;
import com.ccl.keyassistant.entity.MBluetoothDevice;

import java.util.List;
import java.util.Set;

/*
 *项目名     keyassistant
 *包名       com.ccl.keyassistant.adapter
 *文件名     ScanAdapter
 *创建者     ccl
 *创建时间   2019-03-12 19:52
 *注解       扫描蓝牙listview的数据适配器
 */

public class ScanAdapter extends BaseAdapter {

    private Context mContext;
    private List<MBluetoothDevice> mDatas;

    public ScanAdapter(Context mContext, List<MBluetoothDevice> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View ve;
        if (view != null) {
            ve = view;
        } else {
            ve = View.inflate(mContext, R.layout.blue_listview_item, null);
        }
        TextView mBlueName = (TextView) ve.findViewById(R.id.blue_name);
        TextView mBlueMac = (TextView) ve.findViewById(R.id.blue_mac);
        MBluetoothDevice mBluetoothDevice = mDatas.get(i);
        BluetoothDevice bluetoothDevice = mBluetoothDevice.getBluetoothDevice();

        int bondState = bluetoothDevice.getBondState();
        String name = bluetoothDevice.getName();
        if (bondState == BluetoothDevice.BOND_BONDED) {
            name = name + " bind";
        }
        if (TextUtils.isEmpty(name)) {
            name = "unknow";
        }
        mBlueName.setText(name);
        mBlueMac.setText(bluetoothDevice.getAddress());
        return ve;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
}
